package com.jdd.fintech.morse.plugin.util;

import com.jdd.fintech.morse.plugin.constans.OpenApiConstans;
import com.jdd.fintech.morse.plugin.enums.EncrAndDecrAlgorithmTypeEnum;
import com.jdd.fintech.morse.plugin.enums.CustomVersionEnum;
import com.jdd.fintech.morse.plugin.enums.RequestDateDirectionEnum;
import com.jdd.fintech.morse.plugin.plugconfig.MorsePlugValueConfig;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: 公共方法工具类
 * @credit: 2022-03-21 10:19
 * @author: fengcheng
 **/
public class CommonMethodUtils {

    /**
     * 获取请求报文中 业务数据的KEY
     * @param plugValueConfig
     * @return
     */
    public static String getMessageBizDateKey(MorsePlugValueConfig plugValueConfig){
        String jsonKey = OpenApiConstans.RESPONSE_DATA_KEY;
        if(RequestDateDirectionEnum.REQUEST.getCode().equals(plugValueConfig.getRequestDateDirection().getCode())){
            jsonKey = OpenApiConstans.REQUEST_DATA_KEY;
        }

        String dataContentKey = plugValueConfig.getDataContentKey();
        if(StringUtils.isNotBlank(dataContentKey) && OpenApiConstans.PA_DATA_CONTENT_KEY.equals(dataContentKey)){
            //平安普惠报文业务数据KEY
            jsonKey = OpenApiConstans.PA_DATA_CONTENT_KEY;
        } else if(StringUtils.isNotBlank(dataContentKey) && OpenApiConstans.YGC_DATA_CONTENT_KEY.equals(dataContentKey)){
            //云工厂报文业务数据KEY
            jsonKey = OpenApiConstans.YGC_DATA_CONTENT_KEY;
        }
        return jsonKey;
    }

    /**
     * 获取OpenApi定制化版本枚举
     * @param customVersion
     * @return
     */
    public static CustomVersionEnum getCustomerVersion(CustomVersionEnum customVersion){
        //现阶段金条、金城、云地结合、平安惠普，使用同一套加解密逻辑
        if(customVersion.getCode().equals(CustomVersionEnum.G.getCode()) ||
           customVersion.getCode().equals(CustomVersionEnum.JC.getCode()) ||
           customVersion.getCode().equals(CustomVersionEnum.CL.getCode()) ||
           customVersion.getCode().equals(CustomVersionEnum.PAPH.getCode())
        ){
            customVersion = CustomVersionEnum.G;
        }
        return customVersion;
    }


    /**
     * @方法描述: 是否是对称加解密，对称加密返回：true；非对称加解密返回：false
     * @方法名称: CryptoUtils.IsItSymmetricalEncryptionAndDecryption
     * @方法操作人: ext.fengcheng
     * @方法生成时间: 2022/10/20 14:21
     * @方法执行参数: [plugValueConfig]
     * @方法返回参数: boolean
     * @方法产生异常:
     **/
    public static boolean isItSymmetricalEncryptionAndDecryption(MorsePlugValueConfig plugValueConfig){
        boolean flag;
        EncrAndDecrAlgorithmTypeEnum encyAndDecyType = plugValueConfig.getEncyAndDecyType();
        switch (encyAndDecyType){
            case AES:
            case DES:
            case TRIPLE_DES:
            case SM4:
                flag = true;
                break;
            case PUBLIC_RSA:
            case PRIVATE_RSA:
                flag = false;
                break;
            default:
                throw new IllegalArgumentException("暂不支持该种加密方式");
        }
        return flag;
    }
}
